//
//  Player.m
//  MusicPlayer-two
//
//  Created by lanou3g on 15/8/21.
//  Copyright (c) 2015年 大展宏图. All rights reserved.
//

#define  HJLog(...) NSLog(__VA_ARGS__)
#import "Player.h"
#import "PlayerManager.h"
#import "NSString+NSString_Extension.h"

@interface Player ()




@end
@implementation Player



-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addAllViews];
    }
    return self;
    
}



- (void)addAllViews{

    

    self.playerScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 300)];
    self.playerScrollView.backgroundColor = [UIColor cyanColor];
    _playerScrollView.contentSize = CGSizeMake(self.frame.size.width * 2, self.playerScrollView.frame.size.height);
    _playerScrollView.bounces = NO; // 边界回弹
    _playerScrollView.directionalLockEnabled = YES;
    [self addSubview:_playerScrollView];
   

    
    self.ImageAndTableView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width * 2, self.playerScrollView.frame.size.height)];
    _ImageAndTableView.backgroundColor = [UIColor whiteColor];
    _buleImageView = [[UIImageView alloc]initWithFrame:self.ImageAndTableView.bounds];
    [_ImageAndTableView addSubview:_buleImageView];
    
    [_playerScrollView addSubview:_ImageAndTableView];
    
    self.singerImage = [[UIImageView alloc]initWithFrame:CGRectMake(60, 40, 250, 250)];
    self.singerImage.backgroundColor = [UIColor yellowColor];
    self.singerImage.layer.masksToBounds = YES;
    _singerImage.layer.cornerRadius = self.singerImage.frame.size.width / 2 ;
    [_ImageAndTableView addSubview:_singerImage];
    
    self.lyricTableView = [[UITableView alloc]initWithFrame:CGRectMake(self.frame.size.width, 0, self.playerScrollView.frame.size.width, self.playerScrollView.frame.size.height)];
    [_ImageAndTableView addSubview:_lyricTableView];
    
    [self addSubview:_playerScrollView];
    
    self.playerView = [[UIView alloc]initWithFrame:CGRectMake(0, self.playerScrollView.frame.size.height, self.frame.size.width, self.frame.size.height - 300)];
//    self.playerView.backgroundColor = [UIColor whiteColor];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.playerView.bounds];
    imageView.image = [UIImage imageNamed:@"bluepic"];
    
    [_playerView addSubview:imageView];
    
     [self addSubview:_playerView];
    
    
    self.currentTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 30, 50, 30)];
//    self.currentTimeLabel.backgroundColor = [UIColor darkTextColor];
    self.currentTimeLabel.text = @"00:00";
    [self.playerView addSubview:_currentTimeLabel];
    
    self.timeSlider = [[UISlider alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.currentTimeLabel.frame) + 20, 30, 175, 30)];
    [self.timeSlider addTarget:self action:@selector(didTimerSliderChanged:) forControlEvents:UIControlEventValueChanged];
    [self.playerView addSubview:_timeSlider];
    
    self.totalTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.timeSlider.frame) + 20, 30, self.currentTimeLabel.frame.size.width, self.currentTimeLabel.frame.size.height)];
    self.totalTimeLabel.text = @"04:25";
//    self.totalTimeLabel.backgroundColor = [UIColor purpleColor];
    [self.playerView addSubview:_totalTimeLabel];
    
    self.volumeSlider = [[UISlider alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.timeSlider.frame), CGRectGetMaxY(self.timeSlider.frame) + 30, self.timeSlider.frame.size.width, self.timeSlider.frame.size.height)];
    self.volumeSlider.minimumValue = 0;
    self.volumeSlider.maximumValue = 10.0;
    self.volumeSlider.value = 5.0 ;
    
    [self.volumeSlider addTarget:self action:@selector(didvalumnSliderChanged:) forControlEvents:UIControlEventValueChanged];
    [self.playerView addSubview:_volumeSlider];
    
    self.lastButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.lastButton.frame = CGRectMake(80, CGRectGetMaxY(self.volumeSlider.frame) + 20, 50, 20);
    [_lastButton setTitle:@"上一首" forState:UIControlStateNormal];
    [_lastButton addTarget:self action:@selector(lastAction) forControlEvents:UIControlEventTouchUpInside];
    [self.playerView addSubview:_lastButton];
    
    self.playButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _playButton.frame = CGRectMake(CGRectGetMaxX(self.lastButton.frame) + 30, CGRectGetMinY(self.lastButton.frame),self.lastButton.frame.size.width, self.lastButton.frame.size.height);
    [_playButton setTitle:@"播放" forState:UIControlStateNormal];
    [_playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.playerView addSubview:_playButton];
    
    self.nextButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _nextButton.frame = CGRectMake(CGRectGetMaxX(self.playButton.frame) + 30, CGRectGetMinY(self.lastButton.frame), self.lastButton.frame.size.width, self.lastButton.frame.size.height);
    [_nextButton setTitle:@"下一首" forState:UIControlStateNormal];
    [_nextButton addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
    [self.playerView addSubview:_nextButton];
    
    self.segment = [[UISegmentedControl alloc]initWithItems:@[@"顺序",@"随机",@"单曲"]];
    _segment.frame = CGRectMake(CGRectGetMinX(self.volumeSlider.frame), CGRectGetMaxY(self.lastButton.frame) + 20, self.volumeSlider.frame.size.width, 30);
    [self.segment addTarget:self action:@selector(CahngedSegment:) forControlEvents:UIControlEventValueChanged];
    _segment.selectedSegmentIndex = 0 ;
    _segment.tintColor = [UIColor colorWithRed:0.01 green:0.8 blue:0.8 alpha:1];
    [self.playerView addSubview:_segment];
    
   
    
    

}


/**
 *  上一首
 */
- (void)lastAction{
    
    [[PlayerManager defaultManager] playLastMusic];

}

- (void)playAction:(UIButton *)sender{
    
    if ([sender.titleLabel.text isEqualToString:@"播放"]) {
        [[PlayerManager defaultManager] musicPlay];
        [sender setTitle:@"暂停" forState:UIControlStateNormal];
    }else if ([sender.titleLabel.text isEqualToString:@"暂停"]){
        [[PlayerManager defaultManager] musicPause];
        [sender setTitle:@"播放" forState:UIControlStateNormal];
    }

}

/**
 *  下一首
 */
- (void)nextAction{

    [[PlayerManager defaultManager] playNextMusic];

}

// 总时间
- (void)changeToatalTimeLabel:(NSString *)str{
    
    self.totalTimeLabel.text = [NSString stringWithMilliSecondsFormat:str];

}

//播放的当前时间
- (void)changeCurrentTimeLabel:(NSString *)str{
    
    self.currentTimeLabel.text = str;
    
}

/**
 *  改变时间滑杆儿的最大值
 */
- (void)changeTimeSliderMaxValue:(NSInteger)maxValue{

    [self.timeSlider setMaximumValue:maxValue];
}

/**
 *  改变滑杆为当前播放的时间
 */
- (void)changeTimeSliderVale:(NSInteger )value{

    [self.timeSlider setValue:value animated:YES];
}

/**
 *  使用滑杆调整时间
 */
- (void)didTimerSliderChanged:(UISlider *)sender{

    [[PlayerManager defaultManager] setToTimeByValue:sender.value];
}

/**
 *  使用滑杆调整音量
 */
- (void)didvalumnSliderChanged:(UISlider *)sender{

    [[PlayerManager defaultManager] musciChangeWithValumn:sender.value];
    
//    HJLog(@"%f",sender.value);
}

/**
 *  歌曲切换
 */
- (void)CahngedSegment:(UISegmentedControl *)sender{

    [PlayerManager defaultManager].playMode = sender.selectedSegmentIndex;

}













@end
